package logic

import (
	"context"
	"elem_svc/models/mysql"
	"fmt"

	"elem_svc/elem"
	"elem_svc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type Login2Logic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLogin2Logic(ctx context.Context, svcCtx *svc.ServiceContext) *Login2Logic {
	return &Login2Logic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录2
func (l *Login2Logic) Login2(in *elem.Login2Request) (*elem.Login2Response, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&findUser)

	if findUser.ID == 0 {
		return nil, fmt.Errorf("账号不存在,请前往注册")
	}

	if in.Password != findUser.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}

	return &elem.Login2Response{
		Id: int64(findUser.ID),
	}, nil
}
